/**
 * Created by jiangzhiyong on 15/10/17.
 */

var Manager = {
    init: function() {
    		$("#gainMessage").click(Manager.send_msg);
        $("#login_btn").click(Manager.login);
    },
    send_msg :function () {
    		var mobile = $("#mobile").val();
    		if (mobile == "") {
    			alert("请输入手机号");
    			return false;
    		}
    		var data = {
    			"mobile" : mobile
    		};
    		var host = "http://t2.7kez.com";
    		$.ajax({
	        url: host + "/cpatcha/sms/send",
	        type: 'post',
	        async: true,
	        data: data,
	        dataType: 'json',
	        crossDomain:true,
	        success: function (result) {
	        		 if (result.code == 0 ) {
	        		 	alert("验证码已发送");
	        		 } else {
	        		 	alert(result.msg);
	        		 }
	        		
	        }
	    });
    },
    //登录，拿到token和sessionID
    login : function() {
    		//读取token，如果有就自动登录，如果没有就跳转到登录页面
    		var mobile = $("#mobile").val();
    		if (mobile == "") {
    			alert("请填写合法手机号");
    			return false;
    		}
    		var cpatcha = $("#cpatcha").val();
    		if (cpatcha == "") {
    			alert("请填写验证码");
    			return false;
    		}
    		var password1 = $("#password1").val();
    		if (password1 == "") {
    			alert("请填写新密码");
    			return false;
    		}
    		var password2 = $("#password2").val();
    		if (password2 == "") {
    			alert("请填写新密码");
    			return false;
    		}
    		if (password1 != password2) {
    			alert("2次输入密码不一致");
    			return false;
    		}
		var data = {
			"mobile": mobile,
			"password": calcMD5(password1 + "meetclass" + password1),
			"captcha" : cpatcha
		};
		var host = "http://t2.7kez.com";
	    $.ajax({
	        url: host + "/account/password/reset",
	        type: 'post',
	        async: false,
	        data: data,
	        dataType: 'json',
	        crossDomain:true,
	        success: function (result) {
	            /**
	             * {
					    "code": 0,
					    "data": {
					        "sid": "2de1666ab60b48da86e5f36425a449a4",
					        "token": "7ea7cd7799830ce7955e15504cb2609b",
					        "user": {
					            "birthday": 0,
					            "created_at": 1.44454829e+09,
					            "gender": 1,
					            "id": 3,
					            "mobile": "13800000003",
					            "name": "a2",
					            "nick": "a2",
					            "status": 0,
					            "type": 1
					        }
					    }
					}
	             */
	            if (result.code == 0) {
	            		//写入token、写入session
	            		var token = result.data.token;
	            		var session_id = result.data.sid;
	            		var user_id = result.data.user.id;
	            		var name = result.data.user.name;
	            		$.cookie('token', token, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('session_id', session_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('user_id', user_id, { expires: Number.MAX_VALUE, path: '/' });
	            		$.cookie('name', name, { expires: Number.MAX_VALUE, path: '/' });
	            		//跳转到首页
	            		window.location.href = "index.html";
	            } else {
	            		alert("登录失败! 用户名或密码错误");
	            }
	        }
	    });
    }
};

$(function(){
    Manager.init();
})
